package com.its.us.web.entity.sys;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.JdbcType;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.List;

/**
 * @Author: master
 * @Date: 2020/10/28 18:07
 * @Version: 1.0
 */
@Data
@TableName("sys_user")
@NoArgsConstructor
public class User implements UserDetails {
    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private String id;

    @TableField(value = "username", jdbcType = JdbcType.VARCHAR)
    private String username;
    @TableField(value = "password", jdbcType = JdbcType.VARCHAR)
    private String password;

    @TableField(value = "is_enabled", jdbcType = JdbcType.VARCHAR)
    private String isEnabled;

    public User(String id, String username, String password, List<GrantedAuthority> authorities, String isEnabled) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.authorities = authorities;
        this.isEnabled = isEnabled;
    }

    // security 重写
    @TableField(exist = false, select = false)
    private List<GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 账号是否被禁用
    @Override
    public boolean isEnabled() {
        return (Integer.parseInt(this.isEnabled) != 0) ? false : true;
    }
}
